package models;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import com.avaje.ebean.Expr;

import model.entityes.exception.AllStructuresExeption;

import play.db.ebean.Model;

@Entity
@Table(name = "all_structures")
public class AllStructures extends Model
{
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    protected long id;
    protected String title;
    @Column(columnDefinition = "LONGTEXT")
    protected String description;
    protected String creater;

    protected static String TITLE = "title";
    protected static String DESCRIPTION = "description";
    protected static String ID = "id";
    protected static String CREATER = "creater";

    public List<AllStructures> getAllStructures() throws AllStructuresExeption
    {
	try
	{
	    return AllStructures.find.orderBy(title).findList();
	}
	catch (Exception e)
	{
	    throw new AllStructuresExeption();
	}
    }

    public static AllStructures getStructure(Long id) throws AllStructuresExeption
    {
	try
	{
	    AllStructures strc = AllStructures.find.byId(id);
	    return strc;
	}
	catch (Exception e)
	{
	    throw AllStructuresExeption.errorGetStructure(id);
	}
    }

    public List<AllStructures> searchStructureByTitle(String title)
	    throws AllStructuresExeption
    {
	try
	{
	    return AllStructures.find.where()
		    .ilike(AllStructures.TITLE, "%" + title + "%")
		    .orderBy(AllStructures.TITLE).findList();
	}
	catch (Exception e)
	{
	    throw AllStructuresExeption.errorSearchStructureByTitle(title,
		    e.getMessage());
	}
    }

    public static Model.Finder<Long, AllStructures> find = new Model.Finder<Long, AllStructures>(
	    Long.class, AllStructures.class);

    public static List<AllStructures> getStructuresByFilter(String query)
    {
	query = "%"+query+"%";
	return AllStructures.find.where().or(Expr.like(AllStructures.TITLE, query), Expr.like(AllStructures.DESCRIPTION, query)).findList();
    }
    
    public long getId()
    {
	return id;
    }

    public void setId(long id)
    {
	this.id = id;
    }

    public String getTitle()
    {
	return title;
    }

    public void setTitle(String title)
    {
	this.title = title;
    }

    public String getDescription()
    {
	return description;
    }

    public void setDescription(String description)
    {
	this.description = description;
    }

    public String getCreater()
    {
	return creater;
    }

    public void setCreater(String creater)
    {
	this.creater = creater;
    }

}
